import json
import os
import urllib.parse

class WeChatArticle():
    def __init__(self, row_data):
        self.row_data = row_data
        self.data = []
        self.img_list = []
        
    def article(self, json_file_path):
        self.json_file_path = json_file_path
        res_data = json.loads(self.row_data)
        general_msg_list = json.loads(res_data['general_msg_list'])
        self.parse_msg_list(general_msg_list)
        self.save_data()

    def parse_msg_list(self, general_msg_list):
        if general_msg_list['list'] != []:
            for general_msg in general_msg_list['list']:
                # print(general_msg)
                if general_msg.get('app_msg_ext_info'):
                
                    article_info = {
                        'datetime': general_msg['comm_msg_info']['datetime'],
                        'title': general_msg['app_msg_ext_info']['title'],
                        'digest': general_msg['app_msg_ext_info']['digest'],
                        'content': general_msg['app_msg_ext_info']['content'],
                        'content_url': general_msg['app_msg_ext_info']['content_url'],
                        'source_url': general_msg['app_msg_ext_info']['source_url'],
                        'fileid': general_msg['app_msg_ext_info']['fileid'],
                        'author': general_msg['app_msg_ext_info']['author'],
                        'cover': general_msg['app_msg_ext_info']['cover'],
                    }
                    self.data.append(json.dumps(article_info) + '\n')
                    if general_msg['app_msg_ext_info']['is_multi'] == 1:
                        self.parse_multi_app_msg(general_msg)

    def parse_multi_app_msg(self, general_msg):
        for multi_app_msg_item in general_msg['app_msg_ext_info']['multi_app_msg_item_list']:
            article_info = {
                'datetime': general_msg['comm_msg_info']['datetime'],
                'title': multi_app_msg_item['title'],
                'digest': multi_app_msg_item['digest'],
                'content': multi_app_msg_item['content'],
                'content_url': multi_app_msg_item['content_url'],
                'source_url': multi_app_msg_item['source_url'],
                'fileid': multi_app_msg_item['fileid'],
                'author': multi_app_msg_item['author'],
                'cover': multi_app_msg_item['cover'],
            }
            self.data.append(json.dumps(article_info) + '\n')

    def save_data(self):
        # 写入文件
        if len(self.data) > 0: 
            with open(self.json_file_path, 'a') as json_f:
                json_f.writelines(self.data)

    def insert_img_code(self, img_url):
        img_code = self.get_img_code(img_url)
        if img_code:
            self.img_list.append(img_code)

    def get_img_code(self, img_url):
        img_code = ''
        if img_url:
            img_code = urllib.parse.urlparse(img_url).path.split('/')[2]
        return img_code

    def save_img(self, img_url, img_file_path):
        img_code = self.get_img_code(img_url)
        img_path = img_file_path + '/' + img_code + '.jpeg'
        if os.path.exist(img_path):
            pass
        else:
            with open(img_path, 'wb') as img_f:
                img_f.write(self.data)
                img_f.close()
